/**
v * Copyright 2012 Facebook
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.example.socool_login;

import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import android.app.Activity;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ListView;
import android.widget.Toast;

import com.facebook.Request;
import com.facebook.Response;
import com.facebook.Session;
import com.facebook.android.FacebookError;
import com.facebook.model.GraphUser;

public class Act_FriendsList extends Activity {
	ProgressDialog dialog;
	Session session;

	ListView listView1;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.friends_list);
		 session = Session.getActiveSession();
		 if (session.isOpened()) {
		dialog = ProgressDialog.show(Act_FriendsList.this, "", "Please wait..", true,
				true);
		Request.executeMyFriendsRequestAsync(session,new FriendsRequestListener());
		 }else{
			 
			 Log.d("test", "session is not opened");
		 }
	}

	public class FriendsRequestListener implements
			Request.GraphUserListCallback {

		@Override
		public void onCompleted(List<GraphUser> users, Response response) {
			dialog.dismiss();
			
			Collections.sort(users, new UserComparator() );
			FriendsAdapter adapter = new FriendsAdapter(Act_FriendsList.this, R.layout.friend_row, users);
	        
	        
	        listView1 = (ListView)findViewById(R.id.listView1);
	         
	        View header = (View)getLayoutInflater().inflate(R.layout.friend_row, null);
	        listView1.addHeaderView(header);
	        
	        listView1.setAdapter(adapter);
		}

		public void onFacebookError(FacebookError error) {
			dialog.dismiss();
			Toast.makeText(getApplicationContext(),
					"Facebook Error: " + error.getMessage(), Toast.LENGTH_SHORT)
					.show();
		}

	}
	
	 public class UserComparator implements Comparator {
	     public int compare(Object o1, Object o2) {
	    	 GraphUser r1 = (GraphUser) o1;
	    	 GraphUser r2 = (GraphUser) o2;
	       return r1.getName().compareTo(r2.getName());
	     }
	   }
}
